package com.bestcem.xm.qdes.handler.impor.type;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.qdes.handler.impor.KeyWordHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Component
public class KeywordExcludeGroupHandler implements KeyWordHandler {
    private static final Pattern E_PATTERN = Pattern.compile("\\[组内排他#(.*?)\\]");

    @Override
    public Integer handler(String qType, JSONArray rawItems, JSONObject questionDict) {
        // 排他设置
        //     {id: "36_1",
        //     title: "分组1",
        //     items: [{gid: 37, oid: 1, isExclude: false}, {gid: 38, oid: 2, isExclude: false}]
        //     }
        if (rawItems == null || rawItems.isEmpty()) {
            return 0;
        }

        // 选项排他 标识行没有需要处理的自动
        JSONArray rawOptionExcludes = new JSONArray();
        if (rawItems.size() > 1) {
            for (int i = 1; i < rawItems.size(); i++) {
                rawOptionExcludes.add(rawItems.get(i));
            }
        }
        int totalGidCountTmp = 0;
        if (rawOptionExcludes.size() > 0) {
            questionDict.put("optionsExcludeEffect", true);
        }
        JSONArray retItems = new JSONArray();
        questionDict.put("option_exclude_list", retItems);

        for (int i = 0; i < rawOptionExcludes.size(); i++) {
            JSONArray rawOptionExclude = rawOptionExcludes.getJSONArray(i);
            JSONObject optionExcludeDict = new JSONObject();
            Object optionExcludeTitle = rawOptionExclude.get(0);
            List<String> optionCids = (rawOptionExclude.size() > 1 && StringUtils.isNotEmpty(rawOptionExclude.getString(1).trim()))
                ? Arrays.stream(rawOptionExclude.getString(1).trim().split(",")).collect(Collectors.toList())
                : new ArrayList<>();
            String optionIsExclude = rawOptionExclude.size() > 2 ? rawOptionExclude.getString(2) : "";

            JSONArray itemsJs = new JSONArray();
            for (String cid : optionCids) {
                if (StringUtils.isNotEmpty(cid)) {
                    JSONObject cidJs = new JSONObject();
                    cidJs.put("oid", cid);
                    itemsJs.add(cidJs);
                }
            }
            optionExcludeDict.put("items", itemsJs);

            if (StringUtils.isNotEmpty(optionIsExclude) && optionIsExclude.startsWith("[组内排他#")) {
                Matcher matcher = E_PATTERN.matcher(optionIsExclude);
                if (matcher.find()) {
                    List<String> excludeCids = Arrays.stream(matcher.group(1).split(",")).collect(Collectors.toList());
                    JSONArray items = optionExcludeDict.getJSONArray("items");
                    for (int j = 0; j < items.size(); j++) {
                        JSONObject itemDict = items.getJSONObject(j);
                        itemDict.put("isExclude", excludeCids.contains(itemDict.getString("oid")));
                    }
                }
            }
            optionExcludeDict.put("title", optionExcludeTitle);
            retItems.add(optionExcludeDict);
        }
        return totalGidCountTmp;
    }

    @Override
    public String getType() {
        return "排他设置";
    }
}
